package com.qcby.test;
import java.util.Stack;

public class Queue {
    private Stack<Integer> stack1; // 入队栈
    private Stack<Integer> stack2; // 出队栈

    public Queue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    // 入队操作
    public void push(int x) {
        stack1.push(x);
    }

    // 出队操作
    public int pop() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    // 获取队首元素
    public int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    // 判断队列是否为空
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());  // 返回 1
        System.out.println(queue.pop());   // 返回 1
        System.out.println(queue.empty()); // 返回 false
    }
}